using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour
{
    [System.Serializable]
    public struct GunConfig
    {
        public ABullet.BulletType bulletType;
        public Sprite sprite;

    }

    public GunConfig[] gunConfig;

    public ABullet.BulletType bulletType = ABullet.BulletType.M;

    public float force = 10.0f;
    public Vector2 direction = new Vector2(0.1f, 1f);
    public LayerMask groundLayer;

    private Rigidbody2D rig;
    private Collider2D rigCollider;
    private SpriteRenderer sr;
    private void Awake()
    {
        rig = GetComponent<Rigidbody2D>();
        rigCollider = transform.Find("碰撞刚体").GetComponent<Collider2D>();
        sr = GetComponent<SpriteRenderer>();
    }
    void Start()
    {

    }

    void Update()
    {
    }
    private void FixedUpdate()
    {
        bool onGround = Utility.CheckGround(rigCollider, groundLayer);
        if (onGround && Mathf.Abs(rig.velocity.y) < 0.001)
        {
            rig.velocity = Vector2.zero;
        }
    }

    public void PickUp()
    {
        Destroy(gameObject);
    }

    public void Throw()
    {
        rig.velocity = direction * force;
    }

    public void SetBulletType(ABullet.BulletType bulletType)
    {
        this.bulletType = bulletType;
        sr.sprite = GetSprite(this.bulletType);
    }
    private Sprite GetSprite(ABullet.BulletType bulletType)
    {
        Sprite ret = null;
        foreach (GunConfig config in gunConfig)
        {
            if (config.bulletType == bulletType)
            {
                ret = config.sprite;
                break;
            }

        }
        return ret;
    }
}